import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {

  // 获取基础设置列表
  getBaseRuleList(params) {
    return httpFetch.get(`/api/invoice/discount/base/rules`, params)
  },

  // 保存基础设置规则
  saveBaseRule(params) {
    return httpFetch.post(`/api/invoice/discount/base/rule`, params)
  },

  // 批量删除基础规则
  batchDeleteBaseRule(params, ids) {
    return httpFetch.delete(`/api/invoice/discount/base/rule?${parseParams(params)}`, ids)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  // 核定规则列表
  getDiscountList(params) {
    return httpFetch.get(`/api/invoice/discount/rules`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  // 抵扣规则详情
  getDiscountDetail(id) {
    return httpFetch.get(`/api/invoice/discount/rule?discountRuleId=${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  // 保存核定规则
  saveDiscountRule(params) {
    return httpFetch.post(`/api/invoice/discount/rule`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  // 批量删除核定规则
  batchDeleteDiscountRule(params, ids) {
    return httpFetch.delete(`/api/invoice/discount/rule?${parseParams(params)}`, ids)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

}

const parseParams = queryParams => {
  return Object.keys(queryParams).map(i => `${i}=${queryParams[i] === false ? 'false' : (queryParams[i] || '')}`).join('&')
};
